import * as services from '@/services/article';
import { message } from 'antd';

export default {
  namespace: 'article',
  state: {
    menuData: [],
  },
  effects: {
    /**
     * 查询菜单数据
     * params: { pageSize: 10, pageNumber: 1  }
     */
    *list({ payload: params }, { call, put, select }) {
      const result = yield call(services.list, {
        pageSize: 10,
        pageNumber: 1,
        ...params,
      });
      const { success, msg, data = [] } = result || {};
      !success && message.error(msg || '获取数据失败');
      yield put({
        type: 'save',
        payload: { menuData: Array.isArray(data) ? data : [] },
      });
    },
    /**
     * 新增菜单数据
     */
    *add({ payload: params }, { call, put, select }) {
      const result = yield call(services.add, params);
      const { success, msg } = result || {};
      if (success) {
        message.success('操作成功');
      } else {
        message.error(msg || '保存数据失败');
      }
      // yield put({ type: 'save', payload: { menuData: Array.isArray(data) ? data : [] } });
    },
    /**
     * 菜单详情
     */
    *look({ payload: params }, { call, put, select }) {
      const result = yield call(services.look, params);
      return result;
      // const {success, msg} = result || {}
      // if (success) {
      //   message.success('操作成功');
      // } else {
      //   message.error(msg || '保存数据失败');
      // }
      // // yield put({ type: 'save', payload: { menuData: Array.isArray(data) ? data : [] } });
    },
    /**
     * 更新菜单数据
     */
    *update({ payload: params }, { call, put, select }) {
      const result = yield call(services.update, params);
      const { success, msg } = result || {};
      if (success) {
        message.success('操作成功');
      } else {
        message.error(msg || '保存数据失败');
      }
      // yield put({ type: 'save', payload: { menuData: Array.isArray(data) ? data : [] } });
    },
    /**
     * 删除菜单数据
     */
    *del({ payload: params }, { call, put, select }) {
      const result = yield call(services.del, params);
      const { success, msg } = result || {};
      if (success) {
        message.success('操作成功');
      } else {
        message.error(msg || '保存数据失败');
      }
      // yield put({ type: 'save', payload: { menuData: Array.isArray(data) ? data : [] } });
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  subscriptions: {
    // 订阅，在app.start()即启动项目时被执行
    setup({ dispatch, history }) {
      // return history.listen(({ pathname }) => {
      //   if (pathname === '/') {
      //     dispatch({
      //       type: 'query',
      //     })
      //   }
      // });
    },
  },
};
